package com.lagou.wang.dao.student;

import com.lagou.wang.pojo.Student;
import com.lagou.wang.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * @author wanggege
 * StudentDao实现类
 */
public class StudentDaoImpl implements StudentDao {

    @Override
    public List<Student> allStudent() {
        QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
        String sql = "select * from t_student";

        try {
            List<Student> studentList = qr.query(sql, new BeanListHandler<Student>(Student.class));
            return studentList;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int addStudent(Student student) {
        QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
        String sql = "insert into t_student values (?,?,?,?,?,?)";
        Object[] param = {student.getId(), student.getName(), student.getSex(), student.getBirthday(), student.getEmail(), student.getRemark()};

        try {
            int i = qr.update(sql, param);
            return i;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {

        }
        return 0;
    }

    @Override
    public int updateStudentById(Student student) {
        QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
        String sql = "update t_student  set name=?,sex=?,birthday=?,email=?,remark=? where id=?";
        Object[] param = {student.getName(), student.getSex(), student.getBirthday(), student.getEmail(), student.getRemark(), student.getId()};

        try {
            int i = qr.update(sql, param);
            return i;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {

        }
        return 0;
    }

    @Override
    public int deleteStudentById(int id) {
        QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
        String sql = "delete from t_student where id = ?";

        Object param = id;
        try {
            int i = qr.update(sql, param);
            return i;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public Student queryStudentById(int id) {
        QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
        String sql = "select * from t_student where id=?";
        Object param = id;
        try {
            Student student = qr.query(sql, new BeanHandler<Student>(Student.class), param);
            return student;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Student queryStudentByName(String name) {
        QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
        String sql = "select * from t_student where name=?";
        String param = name;
        try {
            Student student = qr.query(sql, new BeanHandler<Student>(Student.class), param);
            return student;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Student queryStudentByClassId(int cid) {
        QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
        String sql = "select * from t_student where classId = ?";

        Object param = cid;
        try {
            Student student = qr.query(sql, new BeanHandler<Student>(Student.class), param);
            return student;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
